﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace Beryl.OrderControllers
{
    public class OrderPCQuery
    {
        private static OrderPCQuery instance = null;
        private static readonly object uniqueLock = new object();

        public static OrderPCQuery Instance
        {
            get
            {
                lock (uniqueLock)
                {
                    if (instance == null)
                    {
                        instance = new OrderPCQuery();
                    }
                }
                return instance;
            }
        }
        public OrderPCQuery() { }

        public List<string> OrderStatus(string para)
        {
            List<string> list = new List<string>();
            try
            {
                string ret = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(para));//转为json
                if(ret.Equals("[]"))
                {
                    throw new Exception("查询订单状态结果为空");
                }
                list.Add(ret);
            }
            catch(Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }

        //数据列表查询
        public List<string> QueryDataTable(string para)
        {
            List<string> list = new List<string>();
            Newtonsoft.Json.Linq.JArray jArray = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(para);
            try
            {
                var k = jArray[0];
                string value1 = k["value1"].ToString(); //生产批次号 上区间
                string value2 = k["value2"].ToString();//生产订单号
                string value3 = k["value3"].ToString(); //销售单号 
                string value4 = k["value4"].ToString(); //批次状态
                string value5 = k["value5"].ToString(); //生产批次号 下区间
                string value6 = k["value6"].ToString(); //批次类别
                string date1 = k["date1"].ToString();
                string date2 = k["date2"].ToString();
                string cProductDep = k["cProductDep"].ToString(); //当前登录的用户信息
                string value7 = k["value7"].ToString();//物料规格 

                string sql = "select * from View_QueryProBatch where Id > 0 ";

                if (cProductDep == "Z21" || cProductDep == "Z61")
                {
                    sql += string.Format(@"and cProductDepNo = '{0}' ",cProductDep);
                }

                if (value1 != "" && value5 != "")
                {
                    sql += string.Format(@"AND cMOLotNo between '{0}' and '{1}' ",value1,value5);
                }

                if (value1 != "" && value5 == "")
                {
                    sql += string.Format(@"AND cMOLotNo >= '{0}' ", value1);
                }

                if (value1 == "" && value5 != "")
                {
                    sql += string.Format(@"AND cMOLotNo <= '{0}' ",value5);
                }

                if (value2 != "")
                {
                    sql += string.Format(@"AND cOrderNo = '{0}' ",value2);
                }

                if (value3 != "")
                {
                    sql += string.Format(@"AND cParentSONo = '{0}' ",value3);
                }

                if (value4 != "all")
                {
                    sql += string.Format(@"AND cStatusNo = '{0}' ",value4);
                }

                if (value7 != "")
                {
                    sql += string.Format(@"AND cSpecifucation = '{0}' ",value7);
                }

                if (value6 != "全部")
                {
                    sql += string.Format(@"AND cReworkFlag = '{0}' ",value6);
                }

                if (date1 != "" && date2 != "")
                {
                    sql += string.Format(@"and(cMakeTime between '{0}' and '{1}') ",date1,date2);
                }

                if (date1 != "" && date2 == "")
                {
                    sql += string.Format(@"and cMakeTime >= '{0}' ",date1);
                }

                if (date1 == "" && date2 != "")
                {
                    sql += string.Format(@"and cMakeTime <= '{0}' ",date2);
                }

                sql += " ORDER BY cMakeTime,SUBSTRING(cMOLotNo,1,9),cast(SUBSTRING(cMOLotNo,13,5) as int) ";
                string ret = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql));
                if (ret.Equals("[]"))
                {
                    throw new Exception("查询数据为空");
                }
                list.Add(ret);
            }
            catch (Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }

        //更新生产数量
        public List<string> UpdatePlanty(string para)
        {
            List<string> list = new List<string>();
            Newtonsoft.Json.Linq.JArray jArray = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(para);
            try
            {
                var k = jArray[0];
                string cMOLotNo = k["cMOLotNo"].ToString();
                string cMatNo = k["cMatNo"].ToString();
                string dPlanQty = k["dPlanQty"].ToString();
                string sql = string.Format(@"update APS_ProLotOrder set dLotQty = '{0}' where cMOLotNo = '{1}' and cMatNo = '{2}';",dPlanQty,cMOLotNo,cMatNo);
                List<string> sqlArr = new List<string>
                {
                    sql
                };
                bool ok = CommonMethod.Instance.insertSqlList(sqlArr);
                if (!ok)
                {
                    throw new Exception("插入失败");
                }
                list.Add("success");
            }
            catch(Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }

        //查询工艺路线
        public List<string> UpdateProcess(string para)
        {
            List<string> list = new List<string>();
            Newtonsoft.Json.Linq.JArray jArray = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(para);
            try
            {
                var k = jArray[0];
                string cProductDepNo = k["cProductDepNo"].ToString();
                string cMOLotNo = k["cMOLotNo"].ToString();
                var sql = "";
                if(cProductDepNo.Equals("Z21"))
                {
                    sql = string.Format(@"select cOperationNo, cOperationName, cReportFlag, sortNo from APS_Operation_V where cOperationName not like '%固态%' and cOperationName not like '%超电%' order by sortNo,cOperationName");
                }
                else if(cProductDepNo.Equals("Z61"))
                {
                    sql = string.Format(@"select cOperationNo,cOperationName,cReportFlag,sortNo from APS_Operation_V where cOperationName like '%固态%'order by sortNo,cOperationName");
                }
                else
                {
                    sql = string.Format(@"select cOperationNo,cOperationName,cReportFlag,sortNo from APS_Operation_V order by cOperationNo");
                }
                string strPicYZ1 = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql));
                if(strPicYZ1.Equals("[]"))
                {
                    throw new Exception("查询失败");
                }
                list.Add(strPicYZ1);
                //var sql1 = string.Format(@"select cOperationName from APS_ValidRouting where cOrderNo = '{0}' order by cOpSeq", cMOLotNo);
                //strPicYZ1 = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql1));
                //if (strPicYZ1.Equals("[]"))
                //{
                //    throw new Exception("查询失败");
                //}
                //list.Add(strPicYZ1);
            }
            catch(Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }

        //查询工艺路径
        public List<string> QueryProcessPath(string para)
        {
            List<string> list = new List<string>();
            Newtonsoft.Json.Linq.JArray jArray = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(para);
            try
            {
                var k = jArray[0];
                string cProductDepNo = k["cProductDepNo"].ToString();
                string cMOLotNo = k["cMOLotNo"].ToString();
                var sql1 = string.Format(@"select cOperationName from APS_ValidRouting where cOrderNo = '{0}' order by cOpSeq", cMOLotNo);
                string strPicYZ1 = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql1));
                if (strPicYZ1.Equals("[]"))
                {
                    throw new Exception("查询失败");
                }
                list.Add(strPicYZ1);
            }
            catch (Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }

        //更新工艺路线
        public List<string> UpdateProcessData(string para)
        {
            List<string> list = new List<string>();
            Newtonsoft.Json.Linq.JArray jArray = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(para);
            var deleteSql = "";
            var sql = "insert into APS_ValidRouting(cOrderNo,cMatNo,cOrderType,cOpSeq,cOperationNo,cOperationName) values ";
            try
            {
                for(int i = 0; i < jArray.Count; i++)
                {
                    var data = jArray[i].ToString();
                    data = CommonMethod.Instance.JosnChange(data);
                    Newtonsoft.Json.Linq.JArray UpdateData = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(data);
                    var k = UpdateData[0];
                    string value = (i + 10).ToString();
                    string cOpSeq = "00" + value;
                    string cMOLotNo = k["cMOLotNo"].ToString();
                    string cMatNo = k["cMatNo"].ToString();
                    string cLotType = k["cLotType"].ToString();
                    string cOperationNo = k["cOperationNo"].ToString();
                    string cOperationName = k["cOperationName"].ToString();
                    deleteSql += string.Format("delete from APS_ValidRouting where cOrderNo = '{0}' and cMatNo = '{1}' and cOrderType = '{2}';",    cMOLotNo,cMatNo,cLotType);
                    
                    if(i < jArray.Count - 1)
                    {
                        sql += string.Format("('{0}','{1}','{2}','{3}','{4}','{5}'),", cMOLotNo, cMatNo, cLotType, cOpSeq, cOperationNo, cOperationName);
                    }else
                    {
                        sql += string.Format("('{0}','{1}','{2}','{3}','{4}','{5}')", cMOLotNo, cMatNo, cLotType, cOpSeq, cOperationNo, cOperationName);
                    }
                    List<string> sqlArr = new List<string>
                {
                    sql
                };
                    bool ok = CommonMethod.Instance.insertSqlList(sqlArr);
                    if (!ok)
                    {
                        throw new Exception("插入失败");
                    }
                    list.Add("success");
                }
            }catch(Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }

        //关闭订单
        public List<string> CloseOrderMethod(string para)
        {
            List<string> list = new List<string>();
            Newtonsoft.Json.Linq.JArray jArray = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(para);
            try
            {
                var k = jArray[0];
                string remarks = k["remarks"].ToString();
                string closePerson = k["closePerson"].ToString();
                string cMOLotNo = k["cMOLotNo"].ToString();
                string closeTime = CommonMethod.Instance.getTimes();
                string updateSql = string.Format(@"update APS_ProLotOrder set cStatusNo = '0091',remarks = '{0}',closeTime = '{1}',closePerson = '{2}' where cMOLotNo = '{3}';", remarks, closeTime, closePerson, cMOLotNo);
                List<string> sqlArr = new List<string>
                {
                    updateSql
                };
                bool ok = CommonMethod.Instance.insertSqlList(sqlArr);
                if (!ok)
                {
                    throw new Exception("插入失败");
                }
                list.Add("success");
            }
            catch(Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }


    }
}